home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World 2006 November
/
PCWorld_2006-11_cd.bin
/
domacnost a kancelar
/
findgraph
/
fgraph.exe
/
{app}
/
UserModels
/
models.cpp
next >
Wrap
C/C++ Source or Header
|
2004-10-06
|
17KB
|
656 lines
#include "stdafx.h"
#include "Models.h"
#include <math.h>
//-------------------------------------------------------------------------------------------------
////////////////////////////////////////////////////////////////////////////////////////////////
//
// Id must be <= 999
/*
static FuncDesc g_ListFunc[] = // MODELS_LIST_START()
{
//Id, FuncName , FuncForm , FuncCalc , FuncCheck , FuncDefau ,
1, Poly_Name , Poly_Form , Poly_Calc , Poly_Check , Poly_Defau ,
//---------------------------------------------------------
-1, NULL , NULL , NULL , NULL , NULL
}; // MODELS_LIST_END()
*/
MODELS_LIST_START()
// MODELS_LIST_ADD(1, Poly) // use it as example
MODELS_LIST_ADD(2, S2Bind)
MODELS_LIST_ADD(3, S2Comp)
MODELS_LIST_ADD(4, E2Decay)
MODELS_LIST_ADD(5, E2Assoc)
MODELS_LIST_ADD(6, D2Biph)
MODELS_LIST_ADD(7, DAssym)
MODELS_LIST_ADD(8, L2Biph)
MODELS_LIST_ADD(9, S2Gauss)
MODELS_LIST_ADD(10, S2Loren)
MODELS_LIST_END()
static int g_nFunctions = sizeof (g_ListFunc) / sizeof (FuncDesc);
////////////////////////////////////////////////////////////////////////////////////////////////
int FittingModelsList(int nIdList, int *pnIds, int *pNum)
{
int i=0;
while (g_ListFunc[i].m_nId > 0)
{
pnIds[i] = g_ListFunc[i].m_nId;
i++;
};
*pNum = i;
return i;
}
FuncDesc *PFittingDescInId(int nId)
{
int i=0,
n = g_nFunctions;
for (;i<g_nFunctions; i++)
if (nId == g_ListFunc[i].m_nId)
return &g_ListFunc[i];
return NULL;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//
void InitParams(double *pfParams, int nParams)
{
for (int i=0; i<nParams; i++) pfParams[i] = 0.;
}
int Only1Param(double *pfParams, int nParams)
{
for (int i=1; i<nParams; i++) pfParams[i] = 0.;
return 1;
}
int OnlyBParam(double *pfParams, int nParams)
{
pfParams[0] = 0.;
for (int i=2; i<nParams; i++) pfParams[i] = 0.;
return 1;
}
int Only2Param(double *pfParams, int nParams)
{
for (int i=2; i<nParams; i++) pfParams[i] = 0.;
return 2;
}
int Only3Param(double *pfParams, int nParams)
{
for (int i=3; i<nParams; i++) pfParams[i] = 0.;
return 3;
}
int OnlyACDPrm(double *pfParams, int nParams)
{
pfParams[1] = 0.; // B
for (int i=4; i<nParams; i++) pfParams[i] = 0.;
return 3;
}
int Only4Param(double *pfParams, int nParams)
{
for (int i=4; i<nParams; i++) pfParams[i] = 0.;
return 4;
}
int Only5Param(double *pfParams, int nParams)
{
for (int i=5; i<nParams; i++) pfParams[i] = 0.;
return 5;
}
int Only6Param(double *pfParams, int nParams)
{
for (int i=6; i<nParams; i++) pfParams[i] = 0.;
return 6;
}
int Only7Param(double *pfParams, int nParams)
{
for (int i=7; i<nParams; i++) pfParams[i] = 0.;
return 7;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//
//------------------------------------------------------------------------------
//
// Fill information string with name of equation.
// It is used in FindGraph <Master of approximations page 3><Functions>.
//
// Input:
// lpszName - buffer TCHAR
// nLen - maximum buffer length
//
// Output:
// lpszName - name of approximation method
//
// return: no
//
// Example:
// lstrcpyn(lpszName, "Polynomial", nLen);
//
void Poly_Name (LPTSTR lpszName, int nLen) {lstrcpyn(lpszName, "Polynomial", nLen);}
// Fill information string with the equation of function.
// It is used in FindGraph <Master of approximations page 3><Left panel>.
//
// Input:
// lpszForm - buffer TCHAR
// nLen - maximum buffer length
//
// Output:
// lpszForm - name of approximation method
//
// return: no
//
// Important: replace parameters:
// 'a'-'aa', 'b'-'bb', 'c'-'cc', 'd'-'dd', 'g'-'gg', 'h'-'hh', 'k'-'kk', 'l'-'ll', 'm'-'mm'
//
// Example:
// lstrcpyn(lpszForm, "y = aa + bb*x", nLen);
//
void Poly_Form (LPTSTR lpszForm, int nLen)
{
lstrcpyn(lpszForm, "y = aa + bb*x + cc*x^2 + dd*x^3 + gg*x^4 + hh*x^5 + kk*x^6", nLen);
}
// Prepare approximation function: x(u) = u, y(u) = f(u).
// This function is called in Simplex to prepare curve fitting.
//
// Input:
// pfParams - array of parameters pfParams[0]=a [A_PARAM], b,c,d,g,h,k,l,m
// nParams - number of parameters in array
// u - floating-point value
//
// Output:
// Values x(u) and y(u) are calculated.
// px - pointer to x value
// py - pointer to y value
//
// return: 1 if success, or -1 if error.
//
// Example:
// *px = u;
// *py = A_PARAM + B_PARAM*u;
//
int Poly_Calc (double *pfParams, int nParams, double u, double *px, double *py)
{
*px = u;
*py = A_PARAM + B_PARAM*u + C_PARAM*u*u + D_PARAM*u*u*u + G_PARAM*u*u*u*u
+ H_PARAM*u*u*u*u*u + K_PARAM*u*u*u*u*u*u;
return 1;
}
// Check and init parameters.
// This function is called in Simplex to prepare curve fitting.
//
// Input:
// pfParams - array of parameters pfParams[0]=a [A_PARAM], b,c,d,g,h,k,l,m
// nParams - number of parameters in array
//
// Output:
// pfParams
//
// return: number of parameters used.
//
// Example:
// return Only2Param(pfParams, nParams);
// Tip:
// Use functions Only1Param, ... Only9Param
//
int Poly_Check(double *pfParams, int nParams)
{
return Only7Param(pfParams, nParams);
}
// Init parameters with default values.
// It is used in FindGraph <Master of approximations page 3><Parameters>.
//
// Input:
// pfParams - array of parameters pfParams[0]=a [A_PARAM], b,c,d,g,h,k,l,m
// nParams - number of parameters in array
// px - array of values: px[0]=Min(x), px[1]=Max(x), px[2]=Medium(x);
// py - array of values: py[0]=Min(y), py[1]=Max(y), py[2]=Medium(y);
// (x,y) points are used in curve fitting.
// bNormed - if TRUE, parameters pfParams are normed to Min(x)-Max(x), Min(y)-Max(y);
// see check box <Master of approximations page 3><Normalize X>.
//
// Output:
// pfParams
//
// return: number of parameters used.
//
// Example:
// A_PARAM = Y_MIN;
// B_PARAM = (Y_MAX-Y_MIN)/(X_MAX-X_MIN);
//
int Poly_Defau(double *pfParams, int nParams, double *px, double *py, BOOL bNormed)
{
InitParams(pfParams, nParams);
if (bNormed)
{
B_PARAM = 1;
}
else
{
A_PARAM = Y_MIN;
B_PARAM = (Y_MAX-Y_MIN)/(X_MAX-X_MIN);
}
return 7;
}
//------------------------------------------------------------------------------
void S2Bind_Name (LPTSTR lpszName, int nLen) {lstrcpyn(lpszName, "Two site binding", nLen);}
void S2Bind_Form (LPTSTR lpszForm, int nLen)
{
lstrcpyn(lpszForm, "y = aa + bb*x/(cc+x) + dd*x/(gg+x)", nLen);
}
int S2Bind_Calc (double *pfParams, int nParams, double u, double *px, double *py)
{
*px = u;
double r1 = C_PARAM+u,
r2 = G_PARAM+u;
if (r1*r2 == 0) return 0;
*py = A_PARAM + B_PARAM*u/r1 + D_PARAM*u/r2;
return 1;
}
int S2Bind_Check(double *pfParams, int nParams)
{
return Only5Param(pfParams, nParams);
}
int S2Bind_Defau(double *pfParams, int nParams, double *px, double *py, BOOL bNormed)
{
InitParams(pfParams, nParams);
if (bNormed)
{
B_PARAM = D_PARAM = 0.5;
C_PARAM = 0.25;
G_PARAM = 0.75;
}
else
{
A_PARAM = Y_MIN;
B_PARAM = D_PARAM = 0.5*(Y_MAX-Y_MIN);
C_PARAM = 0.75*X_MIN+0.25*X_MAX;
G_PARAM = 0.25*X_MIN+0.75*X_MAX;
}
return 5;
}
//------------------------------------------------------------------------------
void S2Comp_Name (LPTSTR lpszName, int nLen) {lstrcpyn(lpszName, "Two site competition", nLen);}
void S2Comp_Form (LPTSTR lpszForm, int nLen)
{
lstrcpyn(lpszForm, "y = aa + bb*cc/(1+10^(x-dd))+ bb*(1-cc)/(1+10^(x-gg))", nLen);
}
int S2Comp_Calc (double *pfParams, int nParams, double u, double *px, double *py)
{
*px = u;
if (C_PARAM < 0.) C_PARAM = 0.;
if (C_PARAM > 1.) C_PARAM = 1.;
double f1 = pow(10, u-D_PARAM),
f2 = pow(10, u-G_PARAM);
*py = A_PARAM + B_PARAM*C_PARAM/(1+f1) + B_PARAM*(1-C_PARAM)/(1+f2);
return 1;
}
int S2Comp_Check(double *pfParams, int nParams)
{
return Only5Param(pfParams, nParams);
}
int S2Comp_Defau(double *pfParams, int nParams, double *px, double *py, BOOL bNormed)
{
InitParams(pfParams, nParams);
if (bNormed)
{
B_PARAM = 1.0;
C_PARAM = 0.5;
D_PARAM = 0.25;
G_PARAM = 0.75;
}
else
{
A_PARAM = Y_MIN;
B_PARAM = Y_MAX-Y_MIN;
C_PARAM = 0.5;
D_PARAM = 0.75*X_MIN+0.25*X_MAX;
G_PARAM = 0.25*X_MIN+0.75*X_MAX;
}
return 5;
}
//------------------------------------------------------------------------------
void E2Decay_Name (LPTSTR lpszName, int nLen) {lstrcpyn(lpszName, "Two phase exp. decay", nLen);}
void E2Decay_Form (LPTSTR lpszForm, int nLen)
{
lstrcpyn(lpszForm, "y = aa + bb*exp(-cc*x)+ dd*exp(-gg*x)", nLen);
}
int E2Decay_Calc (double *pfParams, int nParams, double u, double *px, double *py)
{
*px = u;
if (C_PARAM < 0.) C_PARAM = 0.;
if (G_PARAM < 0.) G_PARAM = 0.;
*py = A_PARAM + B_PARAM*exp(-C_PARAM*u) + D_PARAM*exp(-G_PARAM*u);
return 1;
}
int E2Decay_Check(double *pfParams, int nParams)
{
return Only5Param(pfParams, nParams);
}
int E2Decay_Defau(double *pfParams, int nParams, double *px, double *py, BOOL bNormed)
{
InitParams(pfParams, nParams);
if (bNormed)
{
B_PARAM =
D_PARAM = 1.0;
C_PARAM = 0.3;
G_PARAM = 3.0;
}
else
{
A_PARAM = Y_MIN;
B_PARAM =
D_PARAM = Y_MAX-Y_MIN;
C_PARAM = 0.3/(X_MAX-X_MIN);
G_PARAM = 3.0/(X_MAX-X_MIN);
}
return 5;
}
//------------------------------------------------------------------------------
void E2Assoc_Name (LPTSTR lpszName, int nLen) {lstrcpyn(lpszName, "Two phase exp. association", nLen);}
void E2Assoc_Form (LPTSTR lpszForm, int nLen)
{
lstrcpyn(lpszForm, "y = aa + bb*(1-exp(-cc*x))+ dd*(1-exp(-gg*x))", nLen);
}
int E2Assoc_Calc (double *pfParams, int nParams, double u, double *px, double *py)
{
*px = u;
if (C_PARAM < 0.) C_PARAM = 0.;
if (G_PARAM < 0.) G_PARAM = 0.;
*py = A_PARAM + B_PARAM*(1-exp(-C_PARAM*u)) + D_PARAM*(1-exp(-G_PARAM*u));
return 1;
}
int E2Assoc_Check(double *pfParams, int nParams)
{
return Only5Param(pfParams, nParams);
}
int E2Assoc_Defau(double *pfParams, int nParams, double *px, double *py, BOOL bNormed)
{
InitParams(pfParams, nParams);
if (bNormed)
{
B_PARAM =
D_PARAM = 1.0;
C_PARAM = 0.3;
G_PARAM = 3.0;
}
else
{
A_PARAM = Y_MIN;
B_PARAM =
D_PARAM = Y_MAX-Y_MIN;
C_PARAM = 0.3/(X_MAX-X_MIN);
G_PARAM = 3.0/(X_MAX-X_MIN);
}
return 5;
}
//------------------------------------------------------------------------------
void D2Biph_Name (LPTSTR lpszName, int nLen) {lstrcpyn(lpszName, "Dose-response biphasic", nLen);}
void D2Biph_Form (LPTSTR lpszForm, int nLen)
{
lstrcpyn(lpszForm, "y = aa + bb*cc/(1+10^((x-dd)*gg))+ bb*(1-cc)/(1+10^((x-kk)*ll))", nLen);
}
int D2Biph_Calc (double *pfParams, int nParams, double u, double *px, double *py)
{
*px = u;
if (C_PARAM < 0.) C_PARAM = 0.;
if (C_PARAM > 1.) C_PARAM = 1.;
double f1 = pow(10, (u-D_PARAM)*G_PARAM),
f2 = pow(10, (u-K_PARAM)*L_PARAM);
*py = A_PARAM + B_PARAM*C_PARAM/(1+f1) + B_PARAM*(1-C_PARAM)/(1+f2);
return 1;
}
int D2Biph_Check(double *pfParams, int nParams)
{
return Only7Param(pfParams, nParams);
}
int D2Biph_Defau(double *pfParams, int nParams, double *px, double *py, BOOL bNormed)
{
InitParams(pfParams, nParams);
if (bNormed)
{
B_PARAM = 1.0;
C_PARAM = 0.5;
D_PARAM = 0.25;
K_PARAM = 0.75;
G_PARAM =
L_PARAM = 0.5;
}
else
{
A_PARAM = Y_MIN;
B_PARAM = Y_MAX-Y_MIN;
C_PARAM = 0.5;
D_PARAM = 0.75*X_MIN+0.25*X_MAX;
K_PARAM = 0.25*X_MIN+0.75*X_MAX;
G_PARAM =
L_PARAM = 0.5/(X_MAX-X_MIN);
}
return 7;
}
//------------------------------------------------------------------------------
void DAssym_Name (LPTSTR lpszName, int nLen) {lstrcpyn(lpszName, "Dose-response asymmetric", nLen);}
void DAssym_Form (LPTSTR lpszForm, int nLen)
{
lstrcpyn(lpszForm, "y = aa + bb/(1+10^((x-dd)*gg))^cc)", nLen);
}
int DAssym_Calc (double *pfParams, int nParams, double u, double *px, double *py)
{
*px = u;
if (C_PARAM < 0.) C_PARAM = 0.;
double f1 = pow(10, (u-D_PARAM)*G_PARAM),
f2 = pow(1+f1, C_PARAM);
*py = A_PARAM + B_PARAM / f2;
return 1;
}
int DAssym_Check(double *pfParams, int nParams)
{
return Only5Param(pfParams, nParams);
}
int DAssym_Defau(double *pfParams, int nParams, double *px, double *py, BOOL bNormed)
{
InitParams(pfParams, nParams);
if (bNormed)
{
B_PARAM = 1.0;
C_PARAM = 1.0;
D_PARAM = 0.0;
G_PARAM = 0.5;
}
else
{
A_PARAM = Y_MIN;
B_PARAM = Y_MAX-Y_MIN;
C_PARAM = 1.0;
D_PARAM = X_MIN;
G_PARAM = 0.5/(X_MAX-X_MIN);
}
return 5;
}
//------------------------------------------------------------------------------
void L2Biph_Name (LPTSTR lpszName, int nLen) {lstrcpyn(lpszName, "Logistic biphasic", nLen);}
void L2Biph_Form (LPTSTR lpszForm, int nLen)
{
lstrcpyn(lpszForm, "y = aa + bb*cc/(1+exp(-(x-dd)*gg))+ bb*(1-cc)/(1+exp(-(x-dd)*hh))", nLen);
}
int L2Biph_Calc (double *pfParams, int nParams, double u, double *px, double *py)
{
*px = u;
if (C_PARAM < 0.) C_PARAM = 0.;
if (C_PARAM > 1.) C_PARAM = 1.;
double f1 = 1.+exp(-(u-D_PARAM)*G_PARAM),
f2 = 1.+exp(-(u-D_PARAM)*H_PARAM);
*py = A_PARAM + B_PARAM*C_PARAM/(1+f1) + B_PARAM*(1-C_PARAM)/(1+f2);
return 1;
}
int L2Biph_Check(double *pfParams, int nParams)
{
return Only6Param(pfParams, nParams);
}
int L2Biph_Defau(double *pfParams, int nParams, double *px, double *py, BOOL bNormed)
{
InitParams(pfParams, nParams);
if (bNormed)
{
B_PARAM = 1.0;
C_PARAM = 0.5;
D_PARAM = 0.0;
G_PARAM = 0.25;
H_PARAM = 0.75;
}
else
{
A_PARAM = Y_MIN;
B_PARAM = Y_MAX-Y_MIN;
C_PARAM = 0.5;
D_PARAM = X_MIN;
G_PARAM = 0.25/(X_MAX-X_MIN);
H_PARAM = 0.75/(X_MAX-X_MIN);
}
return 6;
}
//------------------------------------------------------------------------------
void S2Gauss_Name (LPTSTR lpszName, int nLen) {lstrcpyn(lpszName, "Sum of two Gaussians", nLen);}
void S2Gauss_Form (LPTSTR lpszForm, int nLen)
{
lstrcpyn(lpszForm, "y = aa + bb*exp(-0.5*((x-cc)/dd)^2)+ gg*exp(-0.5*((x-hh)/kk)^2)", nLen);
}
int S2Gauss_Calc (double *pfParams, int nParams, double u, double *px, double *py)
{
*px = u;
double r ,
s1 = 0.,
s2 = 0.;
if (D_PARAM != 0)
{
r = (u-C_PARAM)/D_PARAM;
s1= exp(-0.5*r*r);
}
if (K_PARAM != 0)
{
r = (u-H_PARAM)/K_PARAM;
s2= exp(-0.5*r*r);
}
*py = A_PARAM + B_PARAM*s1 + G_PARAM*s2;
return 1;
}
int S2Gauss_Check(double *pfParams, int nParams)
{
return Only7Param(pfParams, nParams);
}
int S2Gauss_Defau(double *pfParams, int nParams, double *px, double *py, BOOL bNormed)
{
InitParams(pfParams, nParams);
if (bNormed)
{
B_PARAM =
G_PARAM = 0.5;
D_PARAM =
K_PARAM = 0.5;
C_PARAM = 0.25;
H_PARAM = 0.75;
}
else
{
A_PARAM = Y_MIN;
B_PARAM =
G_PARAM = (Y_MAX-Y_MIN)*0.5;
D_PARAM =
K_PARAM = (X_MAX-X_MIN)*0.5;
C_PARAM = 0.75*X_MIN+0.25*X_MAX;
H_PARAM = 0.25*X_MIN+0.75*X_MAX;
}
return 7;
}
//------------------------------------------------------------------------------
void S2Loren_Name (LPTSTR lpszName, int nLen) {lstrcpyn(lpszName, "Sum of two Lorentizian", nLen);}
void S2Loren_Form (LPTSTR lpszForm, int nLen)
{
lstrcpyn(lpszForm, "y = aa + bb/(1+((x-cc)/dd)^2)+ gg/(1+((x-hh)/kk)^2)", nLen);
}
int S2Loren_Calc (double *pfParams, int nParams, double u, double *px, double *py)
{
*px = u;
double r ,
s1 = 0.,
s2 = 0.;
if (D_PARAM != 0)
{
r = (u-C_PARAM)/D_PARAM;
s1= 1/(1+r*r);
}
if (K_PARAM != 0)
{
r = (u-H_PARAM)/K_PARAM;
s2= 1/(1+r*r);
}
*py = A_PARAM + B_PARAM*s1 + G_PARAM*s2;
return 1;
}
int S2Loren_Check(double *pfParams, int nParams)
{
return Only7Param(pfParams, nParams);
}
int S2Loren_Defau(double *pfParams, int nParams, double *px, double *py, BOOL bNormed)
{
InitParams(pfParams, nParams);
if (bNormed)
{
B_PARAM =
G_PARAM = 0.5;
D_PARAM =
K_PARAM = 0.5;
C_PARAM = 0.25;
H_PARAM = 0.75;
}
else
{
A_PARAM = Y_MIN;
B_PARAM =
G_PARAM = (Y_MAX-Y_MIN)*0.5;
D_PARAM =
K_PARAM = (X_MAX-X_MIN)*0.5;
C_PARAM = 0.75*X_MIN+0.25*X_MAX;
H_PARAM = 0.25*X_MIN+0.75*X_MAX;
}
return 7;
}